﻿
using Server.Framework.Message;
using Server.Framework.Processor;
using Server.Framework.Util;
using Server.Service;
using SuperSocket.SocketBase;
using System;

namespace Server.Framework.Socket
{
    /// <summary>
    /// Author: Leil
    /// Description: 服务启动类
    /// CreateTime: 2020/5/11 16:17:11
    /// </summary>
    class ApplicationServer
    {
        private static ApplicationServer instance;

        private ApplicationServer() { }

        public static ApplicationServer Instance()
        {
            if (instance == null)
            {
                instance = new ApplicationServer();
            }
            return instance;
        }

        public ProtobufAppServer AppServer { get; private set; }

        /// <summary>
        /// 启动服务器
        /// </summary>
        public void Start()
        {
            AppServer = new ProtobufAppServer();
            if (!AppServer.Setup(7777))
            {
                Console.WriteLine("Failed to setup server!");
                Console.ReadKey();
                return;
            }

            // 注册消息接收
            AppServer.NewRequestReceived += Received;
            AppServer.NewSessionConnected += SessionConnected;
            AppServer.SessionClosed += SessionClosed;

            if (!AppServer.Start())
            {
                Console.WriteLine("Failed to start server!");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("Server start Success!!! on 7777");
            LogHelper.WriteLog("Server start Success!!! on 7777");
        }

        /// <summary>
        /// 消息接收处理
        /// </summary>
        /// <param name="session">当前通信session</param>
        /// <param name="requestInfo">消息体</param>
        private void Received(ProtobufAppSession session, ProtobufRequestInfo requestInfo)
        {
            RequestBaseMessage message = requestInfo.Body;
            IProcessor processor = ProcessorManager.GetProcessor(message.MsgId);
            if (processor != null)
            {
                processor.Process(session, message);
                return;
            }
            Console.WriteLine("Not found message processor:messageId【{0:x}】", message.MsgId);
        }

        /// <summary>
        /// 新的session接入
        /// </summary>
        /// <param name="session">客户端session</param>
        private void SessionConnected(ProtobufAppSession session)
        {
            Console.WriteLine($"New Session:{session.RemoteEndPoint}");
        }

        /// <summary>
        /// session关闭
        /// </summary>
        /// <param name="session">关闭的session</param>
        /// <param name="reason">关闭的原因</param>
        private void SessionClosed(ProtobufAppSession session, CloseReason reason)
        {
            Console.WriteLine($"Session Close:{session.RemoteEndPoint}");
            Console.WriteLine($"Close Reason:{reason}");
            if (session.User != null)
            {
                // 将用户从内存中移除
                UserService.Instance().RemoveUserSession(session.User.ID);
                LoginLogService.Instance().UpdateLogoutLog(session.User.ID);
            }
        }


    }
}
